﻿// Copyright (c) Inossem. All rights reserved.
// Licensed under the LGPL-3.0 license. See LICENSE file in the project root for full license information.
using DWF.Studio.Share.Log;
using Microsoft.VisualBasic.Activities;
using System;
using System.Activities;
using System.Activities.Debugger.Symbol;
using System.Activities.Presentation;
using System.Activities.Validation;
using System.Activities.XamlIntegration;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Xaml;

namespace DWF.Studio.RemoteCtrls.Custom
{
    public class DragDropScriptTemplate : IActivityTemplateFactory
    {
        public static string ScriptName
        {
            set;
            get;
        }

        public Activity Create(DependencyObject target)
        {
            //此处存在硬编码。
            Activity activity = null;
            try
            {
                var ext = Path.GetExtension(ScriptName).ToLower();
                if (ext == ".xaml")
                {
                    var type = Type.GetType("DWF.Activities.WorkFlow.InvokeXamlActivity, DWF.Activities.WorkFlow", false);
                    if (type != null)
                    {
                        var obj = Activator.CreateInstance(type);
                        var propertyName = type.GetProperty("FileName", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
                        propertyName.SetValue(obj, new InArgument<string>(ScriptName));
                        activity = obj as Activity;
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
            }

            ScriptName = null;

            return activity;
        }
    }
}
